package org.codeart.juc.pool;

import java.util.concurrent.*;

public class ThreadPoolExecutorDemo {

    public static final int coolPoolSize = 2;

    public static final int maximumPoolSize = 5;

    public static final int keepAliveTime = 5;

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(
                coolPoolSize,
                maximumPoolSize,
                keepAliveTime,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(10),
                r -> {
                    Thread t = new Thread(r);
                    t.setName("test-ThreadPoolExecutor");
                    return t;
                },
                (r, exec) -> {
                    System.out.println("根据业务情况, 决定编写代码!");
                }
        );
        
        // 线程池处理任务
        executor.execute(() -> {
            System.out.println("没有返回的任务");
        });

        Future<String> future = executor.submit(() -> {
            System.out.println("有返回结果的任务");
            return "result";
        });
        System.out.println(future.get());
        
        executor.shutdown();
    }

}
